using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ViewOnCore.Helper.WPF;
using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Settings;
using SettingsAddIn.Properties;

namespace UI.SettingsPanel
{

    public partial class SettingsGeneralControl : System.Windows.Controls.UserControl, ISettingPanel
    {

        #region Constructor

        public SettingsGeneralControl()
        {
            InitializeComponent();

            btnApplyNow.Click += new RoutedEventHandler(btnApplyNow_Click);
            btnBrowseMediaPath.Click += new RoutedEventHandler(btnBrowseMediaPath_Click);

            Reset();
        }

        #endregion

        #region ISettingPanel Members

        public string Title
        {
            get
            {
                return SettingsAddIn.Properties.Globalization.SettingsPart_General;
            }
        }

        public void Reset()
        {
            //---- Culture
            string name;
            cboCulture.Items.Clear();
            foreach (CultureInfo cultureInfo in Host.SettingsService.Cultures)
            {
                name = cultureInfo.NativeName;
                name = name.Substring(0, 1).ToUpper() + name.Substring(1);

                cboCulture.Items.Add(name);
            }

            name = Host.SettingsService.CurrentCulture.NativeName;
            name = name.Substring(0, 1).ToUpper() + name.Substring(1);
            cboCulture.SelectedValue = name;

            //---- Cache path
            txtMediaPath.Text = Host.SettingsService.CachePath;

            //---- Animation settings
            cboAnimation.Items.Clear();
            if (!Host.UIService.IsShaderSupported)
            {
                AddTransition(TransitionKindWithoutShader.Sliding.ToString());
                AddTransition(TransitionKindWithoutShader.SmoothSliding.ToString());
            }
            else
            {
                //---- With shader
                AddTransition(TransitionKindWithShader.Sliding.ToString());
                AddTransition(TransitionKindWithShader.Fade.ToString());
                AddTransition(TransitionKindWithShader.Ripple.ToString());
                AddTransition(TransitionKindWithShader.Pixelate.ToString());
                AddTransition(TransitionKindWithShader.LineReveal.ToString());
            }

            //---- Animation
            string selection;
            if (Host.UIService.IsShaderSupported)
                selection = Host.UIService.TransitionKindWithShader.ToString();
            else
                selection = Host.UIService.TransitionKindWithoutShader.ToString();

            for (int index = 0; index < cboAnimation.Items.Count; index++)
                if (selection == (string)(((ComboBoxItem)cboAnimation.Items[index]).Content))
                {
                    cboAnimation.SelectedIndex = index;
                    break;
                }
        }

        private void AddTransition(string name)
        {
            ComboBoxItem item = new ComboBoxItem();
            item.Content = name;
            cboAnimation.Items.Add(item);
        }

        public void Apply()
        {
            if (Host.SettingsService.CurrentCulture != Host.SettingsService.Cultures[cboCulture.SelectedIndex])
                Host.SettingsService.CurrentCulture = Host.SettingsService.Cultures[cboCulture.SelectedIndex];

            if (txtMediaPath.Text != Host.SettingsService.CachePath)
                Host.SettingsService.CachePath = txtMediaPath.Text;

            //---- Animation
            if (!Host.UIService.IsShaderSupported)
            {
                Host.UIService.TransitionKindWithoutShader = (TransitionKindWithoutShader)Enum.Parse(typeof(TransitionKindWithoutShader), cboAnimation.SelectionBoxItem.ToString());
                Host.SettingsService.TransitionKindWithoutShader = Host.UIService.TransitionKindWithoutShader.ToString();
            }
            else
            {
                Host.UIService.TransitionKindWithShader = (TransitionKindWithShader)Enum.Parse(typeof(TransitionKindWithShader), cboAnimation.SelectionBoxItem.ToString());
                Host.SettingsService.TransitionKindWithShader = Host.UIService.TransitionKindWithShader.ToString();
            }
        }

        public void Cancel()
        {
            Reset();
        }

        #endregion

        #region Browse media path

        void btnBrowseMediaPath_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowDialog((Desktop)Host.ApplicationDesktop,
                                                new ChildWindowDialogClosedHandler(OnDialogClosed));
        }

        private void OnDialogClosed(ChildWindowDecorator window)
        {
            FolderBrowserDialog dialog = window.Parent as FolderBrowserDialog;

            if (!dialog.IsOk)
                return;

            txtMediaPath.Text = dialog.SelectedPath + "\\";
        }

        #endregion

        #region Button : apply

        void btnApplyNow_Click(object sender, RoutedEventArgs e)
        {
            Apply();
        }

        #endregion

    }
}